
package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;
    @RequestMapping("/getUsernameAndMenus")
    public Result getUsernameAndMenus()throws Exception{

        LinkedHashSet<Menu> menuList=new LinkedHashSet<>();
        Map<String,Object> map=new HashMap<>();
        try{
            User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            menuList=userService.getUsernameAndMenus(user.getUsername());
            /*menuList.add(menu1);*/
            map.put("username",user.getUsername());
            map.put("menuList",menuList);
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, map);
        }catch (Exception e){
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }
    //获取用户名
    @RequestMapping("/getUsername")
    public Result getUsername()throws Exception{
        try{
            User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());
        }catch (Exception e){
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    //根据条件分页查询用户列表
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult result = userService.findPage(queryPageBean);
            return new Result(true,MessageConstant.GET_USERLIST_SUCCESS,result);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_USERLIST_FAIL);
        }
    }

    //添加用户
    @RequestMapping("/add")
    public Result add(@RequestBody com.itheima.pojo.User user,Integer[] roleIds){
        try {
            userService.add(user, roleIds);
            return new Result(true,MessageConstant.ADD_USER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_USER_FAIL);
        }
    }

    //用户信息回显
    @RequestMapping("/findUserInfo")
    public Result findUserInfo(int id){
        try {
            com.itheima.pojo.User user = userService.findById(id);
            return new Result(true,null,user);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,null);
        }
    }

    //更新用户信息
    @RequestMapping("/update")
    public Result update(@RequestBody Map<String,Object> map){
        try {
            userService.update(map);
            return new Result(true,MessageConstant.UPDATE_USER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.UPDATE_USER_FAIL);
        }
    }

    //删除用户
    @RequestMapping("/delete")
    public Result delete(int id){
        try {
            userService.delete(id);
            return new Result(true,MessageConstant.DELETE_USER_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_USER_FAIL);
        }
    }
}
